package com.jxc.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jxc.mapper.ProductMapper;
import com.jxc.mapper.ShopMapper;
import com.jxc.mapper.StockMapper;
import com.jxc.po.Shops;
import com.jxc.po.Stocks;
import com.jxc.util.DBUtil;
import com.jxc.vo.ProductDetail;

@Controller
public class StockSearchController {
	@RequestMapping(value="/stocksearch")
	public void login(String name,PrintWriter out) { 
		System.out.println(name);
		SqlSession sqlSession = null;
		JSONObject jsonobject =null;
		Map map = new HashMap();
		try {
			sqlSession = DBUtil.getSession();
			StockMapper stockMapper = sqlSession.getMapper(StockMapper.class);
			ShopMapper shopMapper = sqlSession.getMapper(ShopMapper.class);
			ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);
			System.out.println(name.length());
			Shops shop=shopMapper.selectbyname(name.toString());
			List<Stocks> ls=new ArrayList<Stocks>();
			List<ProductDetail> lpd=new ArrayList<ProductDetail>();
			System.out.println(shop.getId());
			ls=stockMapper.selectbysid(shop.getId());
			if(ls==null){
				map.put("result", "null");
				jsonobject=JSONObject.fromObject(map);
			}else{
				for(Stocks stock:ls){
					ProductDetail pd=new ProductDetail();
					pd.setNum(stock.getNum());
					pd.setPrice(stock.getPrice());
					pd.setProductid(stock.getProduct_id());
					pd.setProductname(productMapper.selectbyid(stock.getProduct_id()).getName());
					switch(productMapper.selectbyid(stock.getProduct_id()).getState()){
						case 0:pd.setState("普通产品");break;
						case 1:pd.setState("促销产品");break;
					}
					pd.setStyle(productMapper.selectbyid(stock.getProduct_id()).getStyle());
					lpd.add(pd);
				}
				map.put("result", lpd);
				jsonobject=JSONObject.fromObject(map);
			}

		}catch(Exception e){
			System.out.println(e.toString());
		}
		System.out.println(jsonobject);
		out.print(jsonobject);
	}
	@RequestMapping(value="/firststocksearch")
	public void login2(PrintWriter out) {
		System.out.println("namsdfasfsafe");
		SqlSession sqlSession = null;
		JSONObject jsonobject =null;
		Map map = new HashMap();
		try {
			sqlSession = DBUtil.getSession();
			StockMapper stockMapper = sqlSession.getMapper(StockMapper.class);
			ShopMapper shopMapper = sqlSession.getMapper(ShopMapper.class);
			ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);
			List<Shops> lshop=new ArrayList<Shops>();
			lshop=shopMapper.selectA();
			List<Stocks> ls=new ArrayList<Stocks>();
			List<ProductDetail> lpd=new ArrayList<ProductDetail>();
			ls=stockMapper.selectbysid(lshop.get(0).getId());
			if(ls.size()==0){
				map.put("result", "null");
				map.put("shop",lshop);
				jsonobject=JSONObject.fromObject(map);
			}else{
				for(Stocks stock:ls){
					ProductDetail pd=new ProductDetail();
					pd.setNum(stock.getNum());
					pd.setPrice(stock.getPrice());
					pd.setProductid(stock.getProduct_id());
					pd.setProductname(productMapper.selectbyid(stock.getProduct_id()).getName());
					switch(productMapper.selectbyid(stock.getProduct_id()).getState()){
						case 0:pd.setState("普通产品");break;
						case 1:pd.setState("促销产品");break;
					}
					pd.setStyle(productMapper.selectbyid(stock.getProduct_id()).getStyle());
					lpd.add(pd);
				}
				map.put("result", lpd);
				map.put("shop",lshop);
				jsonobject=JSONObject.fromObject(map);
			}

		}catch(Exception e){
			System.out.println(e.toString());
		}
		System.out.println(jsonobject);
		out.print(jsonobject);
	}
}
